
import {
  getMeizituIndex
} from '@/api'
import cheerio from 'cheerio'
export default {
  namespaced: true,
  state: {
    text: 'home',
    systemInfo: null // 系统信息
  },
  mutations: {
    updateSystemInfo (state, data) {
      state.systemInfo = data
    }

  },
  actions: {
    updateText ({ commit }, par) {
      return new Promise(async (resolve, reject) => {
        try {
          /****
               * 请求
               */
        //   const data = await addEnterHomePage(par)
          commit('changeText', par)
        //   resolve(data)
        } catch (e) {
          reject(e)
        }
      })
    },
    getMeizituIndex ({ commit }, par) {
      return new Promise(async (resolve, reject) => {
        try {
          console.log(par)
          const res = await getMeizituIndex(par)
          let $ = cheerio.load(res)
          // MEIZITU
          let content = $('#content .featured-image')
          let data = []
          content.each((idx, item) => {
            let time = item.parent.parent.previousSibling.prev.children[5].childNodes[0].data
            data.push({
              title: item.attribs['alt'],
              banner: item.attribs['data-original'],
              url: item.parent.attribs['href'],
              time
            })
          })
          let nextPage = $('#pagebtn a')
          let nextUrl = nextPage[0].attribs['href']
          resolve({
            items: data,
            next: nextUrl
          })
        } catch (e) {
          reject(e)
        }
      })
    },
    getAiMMTUList ({ commit }, par) {
      return new Promise(async (resolve, reject) => {
        try {
          const res = await getMeizituIndex(par)
          //   console.log(res)
          let $ = cheerio.load(res)
          // MEIZITU
          let content = $('.excerpts').find('.excerpt').toArray()
          //   console.log(content)
          let data = []
          content.forEach((item, idx) => {
            // console.log(item)
            let time = item.children[2].children[1].children[0].data
            data.push({
              time,
              title: item.children[1].children[0].children[0].data,
              banner: item.children[0].children[0].attribs['data-src'],
              url: item.children[0].attribs['href']
            })
          })
          let nextNode = $('.next-page').toArray()
          //   console.log(nextNode)

          let nextUrl = nextNode[0].children[0].attribs['href']
          resolve({
            items: data,
            next: nextUrl
          })
        } catch (e) {
          reject(e)
        }
      })
    },
    getAiMMTUDetails ({ commit }, par) {
      // 详情
      return new Promise(async (resolve, reject) => {
        try {
          const res = await getMeizituIndex(par)
          //   console.log(res)
          let $ = cheerio.load(res)
          // MEIZITU
          let header = $('.article-title').toArray()
          //   console.log(header)
          let data = {
            header: header[0].children[0].data,
            lists: []
          }
          let list = $('.attachment-thumbnail').toArray()
          //   console.log(list)
          list.forEach((item, idx) => {
            data.lists.push(item.attribs['src'])
          })

          console.log(data)
          resolve(data)
        } catch (e) {
          reject(e)
        }
      })
    }
  },
  getters: {
    systemInfo (state) {
      return state.systemInfo
    }
  }
}
